package de.game.graph;

import java.util.ArrayList;

public class NodeImpl implements Node {

	private String name;
	private int id;
	private float weight;
	private ArrayList<Edge> edges = new ArrayList<Edge>();
	private Node parent;    // used for backtracking in a Grid
	
	
	public NodeImpl(){
		
	}
	
	public NodeImpl(String name, int id, float weight){
		this.name = name;
		this.id = id;
		this.weight = weight;
	}
	
	
	@Override
	public Node createNode(String name, int ID, float weight) {
		return new NodeImpl(name, ID, weight);
	}

	@Override
	public ArrayList<Node> getNeighbours() {
		ArrayList<Node> neighbours = new ArrayList<Node>();		
		
		for(int i = 0; i < edges.size(); i++){

			if(edges.get(i).getStartNode().equals(this)){
				neighbours.add(edges.get(i).getEndNode());
			} else {
				neighbours.add(edges.get(i).getStartNode());
			}
		}
		return neighbours;
	}
	
	
	@Override
	public boolean isKid(Node father){
		for(Node a : father.getKids()){
			if(a.equals(this)){
				return true;
			}
		}
		return false;
	}
	
	
	@Override
	public ArrayList<Node> getKids(){
		ArrayList<Node> kids = new ArrayList<Node>();
		for(int i = 0; i < edges.size();i++){
			if(edges.get(i).getStartNode().equals(this)){
				kids.add(edges.get(i).getEndNode());
			}			
		}
		return kids;		
	}
	
	
	
	public static boolean contains(ArrayList<Node> nodes, Node a){
		for(Node t : nodes){
			if(t.getName().equals(a.getName())){
				return true;
			}
		}
		return false;
	}
	
	
	@Override
	public ArrayList<Edge> getEdges() {	
		return edges;
	}

	@Override
	public void addEdge(Edge e) {
		for(Edge a : edges){
			if(a.getStartNode().getName().equals(e.getEndNode().getName()) && 
					a.getEndNode().getName().equals(e.getStartNode().getName())){
				return;
			}
		}
		edges.add(e);
	}
	
	
	@Override
	public void setWeight(float t){
		this.weight = t;
	}
	
	
	@Override
	public void removeEdge(Edge e) {
		
		edges.remove(e);
		
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public int getID() {
		return id;
	}

	@Override
	public float getWeight() {
		return weight;
	}

	
	public String toString(){
	 
		return ("Name of this Node is "+ this.getName()+ " with ID "+ this.getID()+ "\n " +
				" He has "+ edges.size()+ " edges and his weight is "+ this.getWeight());
		
		
	}
	
	@Override
	public Node getParent() {
		return parent;
	}
	
	@Override
	public void setParent(Node parent) {
		this.parent = parent;
	}

	@Override
	public boolean equals(Object a) {
		return this.getName().equals(((Node)a).getName());
	}
	
	
	
	
	
	
	
}
